package com.kx.service_exam.service.impl;

import com.kx.service_exam.dao.ExaminationMapper;
import com.kx.service_exam.exception.BusinessException;
import com.kx.service_exam.model.Examination;
import com.kx.service_exam.model.ExaminationPart;
import com.kx.service_exam.model.ModulePartReal;
import com.kx.service_exam.service.interfaces.IExaminationPartSV;
import com.kx.service_exam.service.interfaces.IExaminationSV;
import com.kx.service_exam.service.interfaces.IModulePartRealSV;
import com.kx.service_exam.vo.add.ExaminationAddVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
public class ExaminationSVImpl implements IExaminationSV {

    @Autowired
    ExaminationMapper examinationMapper;

    @Autowired
    IExaminationPartSV examinationPartSV;

    @Autowired
    IModulePartRealSV modulePartRealSV;

    @Override
    public int insertRecord(Examination record) {
        record.setId(UUID.randomUUID().toString());
        return examinationMapper.insert(record);
    }

    @Override
    public int deleteRecord(String id) {
        if(StringUtils.isEmpty(id)){
            throw new BusinessException("参数[id]不能为空");
        }
        return examinationMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int updateRecord(Examination record) {
        return examinationMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public Examination selectRecordById(String id) {
        if(StringUtils.isEmpty(id)){
            throw new BusinessException("参数[id]不能为空");
        }
        return examinationMapper.selectByPrimaryKey(id);
    }

    @Override
    public void addExamination(ExaminationAddVO examinationAddVO, String operatorId) {
        Examination examination = new Examination();
        BeanUtils.copyProperties(examinationAddVO,examination);
        examination.setId(UUID.randomUUID().toString());
        Date date = new Date();
        examination.setCreate_time(date);
        examination.setModify_time(date);
        examination.setCreator(operatorId);
        examination.setModifier(operatorId);
        this.insertRecord(examination);

        //新增大题部分
        List<ExaminationPart> examinationPartList =  examinationAddVO.getExaminationParts();
        if(examinationPartList ==  null || examinationPartList.isEmpty()) {
            return;
        }
        examinationPartSV.batchInsertExaminationPart(examinationPartList,examination.getId(),operatorId);
//        Set<String> examinationPartIds = new HashSet<String>();// 用于检验 关系表中的数据是否有效
//        for(ExaminationPart examinationPart: examinationPartList) {
//            examinationPartIds.add(examinationPart.getId());
//        }

        // 新增随机模板
        List<ModulePartReal> modulePartReals = examinationAddVO.getModulePartReals();
        if(modulePartReals == null || modulePartReals.isEmpty()) {
            return;
        }
        modulePartRealSV.batchInsertModulePartReal(modulePartReals,examination.getId(),operatorId);
    }
}
